//
//  Fly.m
//  isMan
//
//  Created by Xing Lion on 11-6-19.
//  Copyright 2011年 InspurWorld. All rights reserved.
//

#import "Fly.h"
#import "HelloWorldLayer.h"
#import "BullertSprite.h"
#import "Sysmenu.h"
#import "GameOver.h"

@implementation Fly

@synthesize gameLayer = _gameLayer;
@synthesize bigExplo = _bigExplo;
@synthesize spriteExplodeBig = _spriteExplodeBig;
@synthesize invincible = _invincible;

-(void)CallBack:(id)sender
{

    CCScene *sc = [CCScene node];
    GameOver* game = [GameOver node];
	[sc addChild:game];
    
    HelloWorldLayer* ly= (HelloWorldLayer*)_gameLayer;
    [game setresulTime:ly.time];
	
	[[CCDirector sharedDirector] replaceScene: sc];
}

- (CGRect)rect
{
	// NSLog([self description]);
	return CGRectMake(-rect_.size.width / 2, -rect_.size.height / 2, rect_.size.width, rect_.size.height);
}

-(void) ShowBigExplodeAt:(CGPoint) posAt
{
	int nSize;
	
	nSize = 40;
	
	[_spriteExplodeBig setPosition:posAt];
	[_spriteExplodeBig setVisible:YES];
	
	CCAnimation *animation = [CCAnimation animation];
	for(int i=0;i<14;i++) {
		[animation addFrameWithTexture:_bigExplo.texture rect:CGRectMake(i*nSize, 0, nSize, nSize) ];
	}				
	
    
    id action = [CCAnimate actionWithDuration:1 animation:animation restoreOriginalFrame:NO];
    id acf = [CCCallFuncN actionWithTarget:self selector:@selector(CallBack:)];
	id ac = [CCSequence actions: action,[CCHide action],acf ,nil];
	
	[_spriteExplodeBig runAction:ac];	
	
}


-(void)CheckExplosion
{

    
    if (_invincible == YES) {
        
        return;
    }
    
    HelloWorldLayer* ly = (HelloWorldLayer*)_gameLayer;
    NSArray* arry =ly.array;
    
    for (BullertSprite* item in arry) 
    {
     
        CGRect rc = CGRectMake(self.position.x - self.textureRect.size.width / 2, self.position.y - self.textureRect.size.height /2, self.textureRect.size.width, self.textureRect.size.height);
        
        if (CGRectContainsPoint(rc , item.position)) 
        {
            
            [self setVisible:NO];
            [self unschedule:@selector(CheckExplosion)];
            
            [self ShowBigExplodeAt:self.position];
        }
    }
}

+ (id) FlyWithinLayer:(CCLayer *)layer
{
    

	HelloWorldLayer *ly = (HelloWorldLayer *)layer;
    
    CCSpriteBatchNode *mgr = [CCSpriteBatchNode batchNodeWithFile:@"flight.png" capacity:5];
    [ly addChild:mgr z:0 tag:4];
    
    
    Fly *fly = [Fly spriteWithTexture:mgr.texture rect:CGRectMake(0,0,31,30)];
    fly.position = ccp(240, 30);
    fly.scale = 1.6;
    [mgr addChild:fly z:1 tag:99];
    
    fly.gameLayer = layer;
    
    
    CCAnimation* animation = [CCAnimation animation];
	for(int i=1;i<3;i++) {
        int x= i % 2;
        x++;
        [animation addFrameWithTexture:mgr.texture rect:CGRectMake(x*32, 0, 31, 30) ];
    }		

	
	id action = [CCAnimate actionWithDuration:1 animation:animation restoreOriginalFrame:NO];


    [fly runAction:[CCRepeatForever actionWithAction:action]];	
	
	
    
    fly.bigExplo = [CCSpriteBatchNode batchNodeWithFile:@"exploBig.png" capacity:15];
    [ly addChild:fly.bigExplo z:0];
    fly.spriteExplodeBig = [CCSprite spriteWithTexture:fly.bigExplo.texture rect:CGRectMake(0,0,40,40)];
    [fly.bigExplo addChild:fly.spriteExplodeBig z:1 tag:5];
    fly.spriteExplodeBig.position = ccp(240, 160);
    [fly.spriteExplodeBig setVisible:NO];

    
	return fly;
}


-(id)init
{
	if( (self=[super init] )) 
    {
    
    	[self schedule:@selector(CheckExplosion) interval: 1/60];
        
        _invincible = NO;
        
    }
    
    return self;

}




// register
- (void)onEnter
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
	[super onEnter];
}

// logout
- (void)onExit
{
	[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
	[super onExit];
}

- (BOOL)containsTouchLocation:(UITouch *)touch
{
	
	return CGRectContainsPoint(self.rect , [self convertTouchToNodeSpaceAR:touch]);
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
	if ( ![self containsTouchLocation:touch] ) return NO;
	
	return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
	
	CGPoint touchPoint = [touch locationInView:[touch view]];
	touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
	
	self.position = CGPointMake(touchPoint.x, touchPoint.y);
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
}


- (void) dealloc
{
    
	[super dealloc];
}

-(CGRect)getspriteRect
{

    CGRect rc = CGRectMake(self.position.x - self.textureRect.size.width / 2, self.position.y - self.textureRect.size.height /2, self.textureRect.size.width, self.textureRect.size.height);
    
    return rc;

}




@end
